package com.china08.yxyapi.api.manage.tec.teaching.mistakes;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureCatalog;
import com.china08.yxyapi.entity.mg.mistakes.TeaCenterMistakes;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLectureCatalogRepository;
import com.china08.yxyapi.repository.mg.mistakes.TeaCenterMistakesRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

/**
 * 教师中心任教书籍列表显示
 * 
 * @author Administrator
 *
 */
@CrossOrigin
@RestController
public class YxyMistakesCatalogSchController extends YxyApiBaseController {

    @Autowired
    private YxyLectureCatalogRepository yxyLectureCatalogRepository;

    @Autowired
    private TeaCenterMistakesRepository teaCenterMistakesRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/mistakes/mistakescatalog/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response) {
        YxyProfile yxyProfile = getProfile(request, response);
        String teaId = yxyProfile.getId();
        String schId = yxyProfile.getSchoolId();
        List<YxyLectureCatalog> dataLst = yxyLectureCatalogRepository.findByUserIdAndSchoolId(teaId, schId);

        TeaCenterMistakes centerMistakes = teaCenterMistakesRepository.findOne(teaId + schId);

        List<RespModel> returnLst = new ArrayList<YxyMistakesCatalogSchController.RespModel>();
        for (YxyLectureCatalog yxyLectureCatalog : dataLst) {
            RespModel model = new RespModel();
            model.id = yxyLectureCatalog.getId();
            model.phaseId = yxyLectureCatalog.getPhaseId();
            model.phaseName = yxyLectureCatalog.getPhaseName();
            model.subjectId = yxyLectureCatalog.getSubjectId();
            model.subjectName = yxyLectureCatalog.getSubjectName();
            model.versionId = yxyLectureCatalog.getVersionId();
            model.versionName = yxyLectureCatalog.getVersionName();
            model.bookId = yxyLectureCatalog.getBookId();
            model.semesterName = yxyLectureCatalog.getSemesterName();

            if (centerMistakes != null) {
                if (centerMistakes.getBookId().equals(yxyLectureCatalog.getBookId())) {
                    returnLst.add(0, model);
                } else {
                    returnLst.add(model);
                }
            } else {
                returnLst.add(model);
            }
        }
        return returnLst;
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {
        // id
        public String id;

        // 学段id
        public String phaseId;

        // 学段名称
        public String phaseName;

        // 学科id
        public String subjectId;

        // 学科
        public String subjectName;

        // 版本ID
        public String versionId;

        // 版本
        public String versionName;

        // 册别id
        public String bookId;

        // 册别名称
        public String semesterName;
    }

}
